package com.zks.pageoffice.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PDFCtrl;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Package: com.zks.pageoffice
 * @ClassName: PDFInsertSealController
 * @Author: ZhouKaiShun
 * @CreateTime: 2021/9/10 10:19
 * @Description:
 */
@RestController
@Slf4j
public class PDFInsertSealController {


    @Value("${spring.profiles.active}")
    public String active;

    @Value("${signpath}")
    public String signPath;

    @Value("${sealpath}")
    private String sealPath;


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView showIndex() {
        ModelAndView mv = new ModelAndView("Index");
        return mv;
    }

    /**
     * @description:盖章功能
     * @return:
     * @author: ZhouKaiShun
     * @time: 2021/9/10 13:40
     */

    @GetMapping("/AddSeal")
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
        log.info("准备盖章");
        PDFCtrl pdfCtrl1 = new PDFCtrl(request);
        //此行必须
        pdfCtrl1.setServerPage(request.getContextPath() + "/poserver.zz");
        //设置保存页面
        pdfCtrl1.setSaveFilePage("AddSeal/save");
        // Create custom toolbar
        pdfCtrl1.addCustomToolButton("保存", "Save()", 1);
        pdfCtrl1.addCustomToolButton("加盖印章", "InsertSeal()", 2);

        //此处路径为服务器端文档路径  如果是Linux服务器上的路径，前面需要加“file://”  (这个点找了好久......)
        pdfCtrl1.webOpen(active.equals("prod") ? "file://" + sealPath : sealPath);
        map.put("pageoffice", pdfCtrl1.getHtmlCode("PDFCtrl1"));
        ModelAndView mv = new ModelAndView("AddSeal/PDF1");
        return mv;
    }

    /**
     * @description:签字功能
     * @return:
     * @author: ZhouKaiShun
     * @time: 2021/9/10 13:40
     */
    @GetMapping("AddSign")
    public ModelAndView showWord11(HttpServletRequest request, Map<String, Object> map) {
        log.info("准备签字");
        //PageOfficeCtrl pdfCtrl1 = new PageOfficeCtrl(request);
        PDFCtrl pdfCtrl1 = new PDFCtrl(request);
        //此行必须
        pdfCtrl1.setServerPage(request.getContextPath() + "/poserver.zz");
        //设置保存页面
        pdfCtrl1.setSaveFilePage("AddSign/save");
        // Create custom toolbar
        pdfCtrl1.addCustomToolButton("保存", "Save()", 1);
        pdfCtrl1.addCustomToolButton("签字", "AddHandSign()", 3);
        //此处路径为服务器端文档路径   如果是Linux服务器上的路径，前面需要加“file://”  (这个点找了好久......)
        pdfCtrl1.webOpen(active.equals("prod") ? "file://" + signPath : signPath);
        map.put("pageoffice", pdfCtrl1.getHtmlCode("PDFCtrl1"));
        ModelAndView mv = new ModelAndView("AddSign/PDF1");
        return mv;
    }


    @RequestMapping("/AddSeal/save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        //此处路径为服务器端文档路径
        fs.saveToFile(sealPath);
        fs.close();
    }

    @RequestMapping("/AddSign/save")
    public void save2(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        //此处路径为服务器端文档路径
        fs.saveToFile(signPath);
        fs.close();
    }

}
